package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.system.domain.CyAfterinvestigation;
import com.ruoyi.system.domain.CyQuestionRequirement;
import com.ruoyi.system.service.ICyAfterinvestigationService;
import com.ruoyi.system.service.ICyQuestionRequirementService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;


import java.util.ArrayList;
        import java.util.LinkedHashMap;
        import java.util.List;
        import java.util.Map;
        import org.springframework.web.bind.annotation.DeleteMapping;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.PostMapping;
        import org.springframework.web.bind.annotation.PutMapping;
        import org.springframework.web.bind.annotation.RequestBody;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import com.ruoyi.common.core.controller.BaseController;
        import com.ruoyi.common.core.domain.AjaxResult;
        import com.ruoyi.common.utils.StringUtils;
        import io.swagger.annotations.Api;
        import io.swagger.annotations.ApiImplicitParam;
        import io.swagger.annotations.ApiImplicitParams;
        import io.swagger.annotations.ApiModel;
        import io.swagger.annotations.ApiModelProperty;
        import io.swagger.annotations.ApiOperation;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("燕茹的测试接口")
@RestController
@RequestMapping("/yanru/test")
public class YanruTestController extends BaseController
{

    @Autowired
    private ICyQuestionRequirementService cyQuestionRequirementService;

    @Autowired
    private ICyAfterinvestigationService cyAfterinvestigationService;//

    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin111111111", "11111111111111", "158828888888"));
        users.put(2, new UserEntity(2, "ry111111111111", "11112222", "15666666666"));
    }

    @ApiOperation("燕茹测试获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return error("用户不存在");
        }
    }

    @ApiOperation("燕茹测试保存用户问卷信息")
    @PostMapping("/saveQuestion")
    @ResponseBody
    public AjaxResult saveQuestion(@RequestBody String jsonObject)//JSONObject jsonObject)
    {
        logger.debug("收到用户数据："+jsonObject);
        logger.debug("收到用户数据2："+jsonObject);

        JSONObject obj =  JSON.parseObject(jsonObject);
        logger.debug("收到用户数据3："+obj);
        //数据已经转化
        return AjaxResult.error("保存失败：服务器收到的数据="+obj);
    }

    @ApiOperation("燕茹测试保存需求问卷信息")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @GetMapping("/{userId}")
    @PostMapping("/saveQuestionEntity")
    @ResponseBody
    public AjaxResult saveQuestionEntity(@RequestBody CyQuestionRequirement jsonObject)//JSONObject jsonObject)
    {
        return AjaxResult.success("提交成功，感谢您的反馈！");
    }

    //
    @ApiOperation("燕茹测试保存售后问卷")
    @PostMapping("/saveAfterinvestigationEntity")
    @ResponseBody
    public AjaxResult saveAfterinvestigationEntity(@RequestBody CyAfterinvestigation jsonObject)//JSONObject jsonObject)
    {
        return AjaxResult.success("提交成功，感谢您的反馈！");
    }

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    //测试设置redis key

    @ApiOperation("燕茹测试redis接口")
//    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path", dataTypeClass = Integer.class)
//    @GetMapping("/{userId}")
    @GetMapping("/testRedis/{userId}")
//    public AjaxResult getUser(@PathVariable Integer userId)
//    @PostMapping("/testRedis")
//    @ResponseBody
    public AjaxResult testRedis(@PathVariable  String userId)//JSONObject jsonObject)
    {
       // UserEntity selectByPrimaryKey = attachmentdao.selectByPrimaryKey(id);
       // stringRedisTemplate.opsForValue().append("attachment",JSON.toJSONString(selectByPrimaryKey));
        //ResultMap resultMap = new ResultMap();
     //   resultMap.put("Attachment", selectByPrimaryKey);
//        stringRedisTemplate.opsForValue().append(userId, "张三");

        stringRedisTemplate.opsForValue().set(userId, "张三");
        stringRedisTemplate.opsForValue().set(userId+"null",  "");

        String res = stringRedisTemplate.opsForValue().get(userId);

        return AjaxResult.success("redis测试接口:"+res);
    }
}
